package flc.ast.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;
import com.stark.picselect.entity.SelectMediaEntity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityProAudioBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.utils.UriUtil;

public class ProAudioActivity extends BaseAc<ActivityProAudioBinding> {

    public static String audioPath;
    private CastScreenManager mCastScreenManager;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private long audioLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeft.setText(TimeUtil.getMmss(mediaPlayer.getCurrentPosition()));
            mDataBinding.sbPlay.setProgress(mediaPlayer.getCurrentPosition() / 1000);
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_audio;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProAudioBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);

        mCastScreenManager = CastScreenManager.getInstance();
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivPlay) {   //播放/暂停
            if (mediaPlayer.isPlaying()) {
                mDataBinding.ivPlay.setImageResource(R.drawable.abof);
                mediaPlayer.pause();
                stopTime();
            } else {
                mDataBinding.ivPlay.setImageResource(R.drawable.azant);
                mediaPlayer.start();
                startTime();
            }
        }
    }

    @Override
    protected void initData() {
        //设置播放器
        try {
            setMedia();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        proMusic();
        saveRecord();
    }

    //音乐投屏
    private void proMusic() {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_AUDIO);
        Uri uri = Uri.parse(audioPath);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(audioPath);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    private void setMedia() throws IOException {
        mHandler = new Handler();
        audioLength = MediaUtil.getDuration(audioPath);
        //设置seekbar
        mDataBinding.sbPlay.setMax((int) audioLength / 1000);
        mDataBinding.tvPlayLeft.setText("00:00");
        mDataBinding.tvPlayRight.setText(TimeUtil.getMmss(audioLength));
        mediaPlayer.setDataSource(audioPath);
        mediaPlayer.prepare();
        mediaPlayer.seekTo(1);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvPlayLeft.setText("00:00");
                mDataBinding.sbPlay.setProgress(0);
                mp.seekTo(1);
                stopTime();
            }
        });
        //直接播放音频
        mediaPlayer.start();
        mDataBinding.ivPlay.setImageResource(R.drawable.azant);
        startTime();
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(audioPath);
        List<String> listSp = AppSpUtils.getAudioList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveAudioList(listSp);
        } else {
            AppSpUtils.saveAudioList(listSave);
        }
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopTime();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

}